package pkg

import (
	"context"
	"fmt"
	"github.com/golang-jwt/jwt/v5"
	"time"
	"xiaohongshu-article/global"
)

type MyCustomClaims struct {
	UserId uint32 `form:"user_id" json:"user_id"`
	jwt.RegisteredClaims
}

func CreateJwtToken(userId uint32, dateTime time.Duration, tokenStr string) interface{} {
	claims := MyCustomClaims{
		userId,
		jwt.RegisteredClaims{
			// A usual scenario is to set the expiration time relative to the current time
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(dateTime)),
		},
	}

	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	ss, err := token.SignedString(tokenStr)
	fmt.Println(ss, err)

	global.RDB.Set(context.Background(), fmt.Sprintf("token:%T", token), userId, dateTime)
	
	return ss
}
